package com.xmlt.ui.xmltutils;

import com.xmlt.core.template.TemplateSection;
import com.xmlt.core.template.instance.AbstractInstance;
import com.xmlt.core.template.instance.Instance;
import com.xmlt.core.template.instance.MultiInstance;
import com.xmlt.core.template.instance.Value;
import com.xmlt.core.template.tags.AbstractSectionElementVisitor;
import com.xmlt.core.template.tags.TextTag;
import com.xmlt.core.template.types.IChoiceValidator;
import com.xmlt.core.template.types.IValidator;

public class GuidedDisplayGenerator {
	public GuidedDisplayGenerator() {
	}
	
	public String getDisplayString(Instance inst) {
		TemplateSection section = inst.getTemplate().getDisplaySection();
		SectionElementVisitor visitor = new SectionElementVisitor(inst);
		section.accept(visitor);
		return visitor.getString();
	}
	
	private class SectionElementVisitor extends AbstractSectionElementVisitor {
		private StringBuffer stringBuffer;
		private SectionElementVisitor(AbstractInstance inst) {
			super(inst);
			stringBuffer = new StringBuffer();
		}
		
		public String getString() {
			return stringBuffer.toString();
		}

		public void handleValue(Value value) {
			String displayValue = value.getDisplayValue();
			String contentValue = String.valueOf(value.getValue());
			displayValue = displayValue == null? "" : displayValue;
			IValidator validator = value.getValidator();
			if (validator instanceof IChoiceValidator) {
				String s = "<select id='" + value.getPath()
				+ "' onchange='setFieldValue(this);' "
				+ " class='" + (((Value) value).validate() ? "" : "error")
				+ "'>";
				for (String option : ((IChoiceValidator) validator).getChoices()) {
					s += "<option value='" + option + "' "
							+ (option.equals(contentValue) ? "selected" : "") + ">"
							+ validator.getDisplayValue(option) + "</option>";
				}
				s += "</select>";
				stringBuffer.append(s);
			} else {
				String s = "<input id='" + value.getPath()
						+ "' onchange='setFieldValue(this);' value='" + displayValue
						+ "' class='" + (((Value) value).validate() ? "" : "error")
						+ "'/><br/>";
				stringBuffer.append(s);
			}
		}
		
		public void handleMultiInstanceStart(MultiInstance multiInstance) {
			String path = multiInstance.getPath();
			String s = "<table id ='table_" + path + "' class='multiInstanceTable loop'><tbody>";
			s += "<tr><td colspan='2'><a id='" + path
					+ "' href='#' onclick='addListItem(this)'>add " + path
					+ "</a><br/></td></tr>";
			stringBuffer.append(s);
		}
		
		public void handleMultiInstanceEnd(MultiInstance multiInstance) {
			stringBuffer.append("</tbody></table>");
		}
		
		public void handleMultiInstanceItemStart(AbstractInstance itemInstance, MultiInstance multiInstance, int index) {
			stringBuffer.append("<tr><td>");
		}
		
		public void handleMultiInstanceItemEnd(AbstractInstance itemInstance, MultiInstance multiInstance, int index) {
			String s = "</td><td>";
			String childPath = multiInstance.getPath() + "[" + index + "]";
			s += "<td><a id='" + childPath + "' href='#' onclick='delListItem(this)'>delete " + childPath + "</a></td></tr>";
			
			stringBuffer.append(s);
		}

		@Override
		public void handleTextTag(TextTag textTag) {
			stringBuffer.append(textTag.getText());
		}

		@Override
		public void handleObject(Object o) {
			stringBuffer.append(o);
		}

		@Override
		public void handleException(Exception e) {
			stringBuffer.append("<a href='#' onclick='alert(\"" + e.getMessage() + "\");return false;'>[Error]</a>");
		}
	}
	
}
